'''
Description: 
Author: pdh
Date: 2020-10-14 17:07:42
LastEditors: pdh
LastEditTime: 2020-11-06 18:25:58
FilePath: \pyserver\websocket_server.py
'''
# coding=utf-8

import asyncio
from re import T

import threading
import time


class Test():

    def __init__(self) -> None:
        self.__eventLoop = None
        self.__data = {}

    def __Updata(self):

        asyncio.set_event_loop(self.__eventLoop)
        self.__eventLoop.run_forever()

    async def func1(self):
        print("this is func1")
        await asyncio.sleep(1)
        print("I will end in func1")
        self.__data["func1"] = True 

    async def func2(self):
        print("this is func2")
        await asyncio.sleep(1)
        print("I will end in func2")
        self.__data["func2"] = True
    
    def openThread(self):
        self.__eventLoop = asyncio.new_event_loop()
        task = threading.Thread(target=self.__Updata)
        task.setDaemon(True)
        task.start()

        asyncio.run_coroutine_threadsafe(self.func1(), self.__eventLoop)
        asyncio.run_coroutine_threadsafe(self.func2(), self.__eventLoop)

    def getData(self):
        return self.__data

if "__main__" == __name__:
    # eventLoop = asynciio.new_event_loop()
    tester = Test()
    tester.openThread()

    while True:
        time.sleep(5)
        print(tester.getData())
        print("I am mainThread")